# -*- coding: utf-8 -*-

############################################################################
#
#   Copyright (c) 2020  Bresee, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#############################################################################

"""
http interface api
"""

import logging
import requests
from requests.models import default_hooks
import utils.common.log as log
import json
import time
import datetime

#algorithms_url = "http://intelligent.bresee.cn:33033"  # bresee algorithms platform .bg算法平台
algorithms_url = ''
def post_vlogclip_algorithm_compose(jobInfo, templateList,cameraList) -> (bool,str) :
    """ 发布任务到智能合成算法合成。Issue tasks to intelligent composition algorithm compose"""

    url = algorithms_url+"/techalgo/api/v1/videoclip/compose"
    ret = False
    
    #if len(templateList) >0 and len(cameraList) > 0 :
        #reqdic = {"jobInfo": jobInfo, "templateList": templateList,"cameraList":cameraList }
    #else :
        #reqdic = {"jobInfo": jobInfo }
        
    reqdic = {"jobInfo": jobInfo }
    ### log.logging.info(json.dumps(reqdic))
    try:
        res = requests.post(url, data=json.dumps(reqdic))
        log.logger.info("res: {}".format(res))
    except Exception as e:
        log.logger.warning("error: {}".format(e))
        return ret,""
    else:
        if res.status_code == 200:
            retdic = json.loads(res.text)
            log.logger.info("##### retdic: {}".format(res.text))
            if retdic["success"] is True:
                if "data" in retdic:
                    if "jobId" in retdic["data"] :
                        return True,retdic["data"]
                    else:
                        return False,''
                else :
                    return False,''
            else:
                log.logger.error("*****vidoclip compose job failed {}".format(res.text))
        else:
            log.logger.error("*****vidoclip compose job failed {}".format(res.text))
    return ret,""

def post_vlogclip_algorithm_query(queryparam) -> (bool,str) :
    """Issue tasks to intelligent composition algorithm query """

    url = algorithms_url+"/techalgo/api/v1/videoclip/query"
    ret = False

    reqdic = queryparam
    log.logging.info(json.dumps(reqdic))
    try:
        res = requests.post(url, data=json.dumps(reqdic))
    except Exception as e:
        log.logger.warning("error: {}".format(e))
        return ret,""
    else:
        if res.status_code == 200:
            retdic = json.loads(res.text)
            if retdic["success"] is True:
                if "data" in retdic:
                    return True,retdic["data"]
                else :
                    return ret,""
            else:
                log.logger.error("*****vidoclip query job failed {}".format(res.text))
        else:
            log.logger.error("*****vidoclip query job failed {}".format(res.text))
    return ret,""

def post_vlogclip_products_download(downloadparam) -> (bool,str) :
    """ Issue tasks to intelligent composition algorithm download """

    url = algorithms_url+"/techalgo/api/v1/videoclip/products/download"
    ret = False

    reqdic = downloadparam
    log.logging.info(json.dumps(reqdic))
    try:
        res = requests.post(url, data=json.dumps(reqdic))
    except Exception as e:
        log.logger.warning("error: {}".format(e))
        return ret,""
    else:
        if res.status_code == 200:
            retdic = json.loads(res.text)
            if retdic["success"] is True:
                if "data" in retdic:
                    return True,retdic["data"]
                else :
                    return ret,""
            else:
                log.logger.error("*****vidoclip download video failed {}".format(res.text))
        else:
            log.logger.error("*****vidoclip download video failed {}".format(res.text))
    return ret,""

def post_vlogclip_algorithm_download(downloadparam) -> (bool,str) :
    """Issue tasks to intelligent composition algorithm download """

    url = algorithms_url+"/techalgo/api/v1/videoclip/query"
    ret = False

    reqdic = downloadparam
    log.logging.info(json.dumps(reqdic))
    try:
        res = requests.post(url, data=json.dumps(reqdic))
    except Exception as e:
        log.logger.warning("error: {}".format(e))
        return ret,""
    else:
        if res.status_code == 200:
            retdic = json.loads(res.text)
            if retdic["success"] is True:
                if "data" in retdic:
                    return True,retdic["data"]
                else :
                    return ret,""
            else:
                log.logger.error("*****vidoclip download video failed {}".format(res.text))
        else:
            log.logger.error("*****vidoclip download video failed {}".format(res.text))
    return ret,""

#################################################################################################
#################################################################################################
#################################################################################################
def post_weixin_paydata(dic):
    """Send a copy of bill data to the algorithm """
    
    url = algorithms_url + "/payapi/tech/pay/paymentsave"
    ret = False
    try:
        res = requests.post(url, data=json.dumps(dic), timeout=3)
        print("res: {}".format(res))
    except Exception as e:
        log.logger.warning("error: {}".format(e))
        return False,""
    else:
        if res.status_code == 200:
            ret = json.loads(res.text)
            if ret["success"] is True:
                return True,ret
            else :
                log.logger.error("post algo error!")
    return ret,""


def post_databoard(dic):
   
    url = "http://127.0.0.1:3800/dataapi/analysis/getUserPortrait"
    ret = ""
    try:
        res = requests.post(url, data=json.dumps(dic), timeout=5)
        print("res: {}".format(res))
    except Exception as e:
        log.logger.warning("error: {}".format(e))
        return ret
    else:
        if res.status_code == 200:
            ret = json.loads(res.text)
    return ret

def get_web_getCase(channelId) -> list:
    
    url = f"http://127.0.0.1:33033/api/v1/party/app/case/channel/{channelId}"

    header = {
        "content-type": "application/json",
        "Authorization": "ZTAI5t7StH4JsKGysZfb2XgT",
    }
    ret = {}
    retBool = False
    try:
        res = requests.get(url, headers=header, timeout=5)
    except Exception as e:
        log.logger.warning("error: {}".format(e))
        return retBool,ret
    else:
        if res.status_code == 200:
            retdic = json.loads(res.text)
            if retdic["success"] is True:
                ret = retdic["data"]
                retBool = True
            else:
                log.logger.error("get get_web_getCase failed {}".format(res.text))
    return retBool, ret


def get_web_getAllCase() -> list:
  
    url = f"http://127.0.0.1:33033/api/v1/party/app/case"

    header = {
        "content-type": "application/json",
        "Authorization": "ZTAI5t7StH4JsKGysZfb2XgT",
    }
    ret = {}
    retBool = False
    try:
        res = requests.get(url, headers=header, timeout=5)
    except Exception as e:
        log.logger.warning("error: {}".format(e))
        return retBool, ret
    else:
        if res.status_code == 200:
            retdic = json.loads(res.text)
            if retdic["success"] is True:
                ret = retdic["data"]
                retBool = True
            else:
                log.logger.error("get get_web_getAllCase failed {}".format(res.text))
    return retBool, ret


def get_web_getAllCaseByOperator(operator) -> list:
     
    url = f"http://127.0.0.1:33033/api/v1/party/app/case/operator/{operator}"

    header = {
        "content-type": "application/json",
        "Authorization": "ZTAI5t7StH4JsKGysZfb2XgT",
    }
    ret = {}
    retBool = False
    try:
        res = requests.get(url, headers=header, timeout=5)
    except Exception as e:
        log.logger.warning("error: {}".format(e))
        return retBool, ret
    else:
        if res.status_code == 200:
            retdic = json.loads(res.text)
            if retdic["success"] is True:
                ret = retdic["data"]
                retBool = True
            else:
                log.logger.error("get get_web_getAllCase failed {}".format(res.text))
    return retBool, ret


def get_web_getCase(caseId) -> dict:
    
    url = f"http://127.0.0.1:33033/api/v1/party/app/case/{caseId}"

    header = {
        "content-type": "application/json",
        "Authorization": "ZTAI5t7StH4JsKGysZfb2XgT",
    }
    ret = {}
    retBool = False
    try:
        res = requests.get(url, headers=header, timeout=5)
    except Exception as e:
        log.logger.warning("error: {}".format(e))
        return retBool, ret
    else:
        if res.status_code == 200:
            retdic = json.loads(res.text)
            if retdic["success"] is True:
                ret = retdic["data"]
                retBool = True
            else:
                log.logger.error("get_web_getCase failed {}".format(res.text))
    return retBool, ret


def get_web_getCasePoint(caseId) -> dict:
     
    url = f"http://127.0.0.1:33033/api/v1/party/app/case/{caseId}/point"

    header = {
        "content-type": "application/json",
        "Authorization": "ZTAI5t7StH4JsKGysZfb2XgT",
    }
    ret = {}
    retBool = False
    try:
        res = requests.get(url, headers=header, timeout=5)
    except Exception as e:
        log.logger.warning("error: {}".format(e))
        return retBool, ret
    else:
        if res.status_code == 200:
            retdic = json.loads(res.text)
            if retdic["success"] is True:
                ret = retdic["data"]
                retBool = True
            else:
                log.logger.error("get_web_getCasePoint failed {}".format(res.text))
    return retBool, ret



# --------------------------------------------------------
def synchronization_getCase() -> list:
    
    url = algorithms_url+"/webapi/v1/scenic/case_apply/synchronization"

    header = {
        "content-type": "application/json",
        "Authorization": "ZTAI5t7StH4JsKGysZfb2XgT",
    }
    ret = {}
    retBool = False
    try:
        res = requests.get(url, headers=header, timeout=5)
    except Exception as e:
        log.logger.warning("error: {}".format(e))
        return retBool, ret
    else:
        if res.status_code == 200:
            retdic = json.loads(res.text)
            if retdic["success"] is True:
                ret = retdic["data"]
                retBool = True
            else:
                log.logger.error("get get_web_getCase failed {}".format(res.text))
    return retBool, ret

def synchronization_getdevice() -> list:
    
    url = algorithms_url+"/webapi/v1/scenic/device_apply/synchronization"

    header = {
        "content-type": "application/json",
        "Authorization": "ZTAI5t7StH4JsKGysZfb2XgT",
    }
    ret = {}
    retBool = False
    try:
        res = requests.get(url, headers=header, timeout=5)
    except Exception as e:
        log.logger.warning("error: {}".format(e))
        return retBool, ret
    else:
        if res.status_code == 200:
            retdic = json.loads(res.text)
            if retdic["success"] is True:
                ret = retdic["data"]
                retBool = True
            else:
                log.logger.error("get get_web_getCase failed {}".format(res.text))
    return retBool, ret

def synchronization_getother(type) -> list:
    
    url = algorithms_url+"/webapi/v1/scenic/device_apply/synchronization/type/"+str(type)

    header = {
        "content-type": "application/json",
        "Authorization": "ZTAI5t7StH4JsKGysZfb2XgT",
    }
    ret = {}
    retBool = False
    try:
        res = requests.get(url, headers=header, timeout=5)
    except Exception as e:
        log.logger.warning("error: {}".format(e))
        return retBool, ret
    else:
        if res.status_code == 200:
            retdic = json.loads(res.text)
            if retdic["success"] is True:
                ret = retdic["data"]
                retBool = True
            else:
                log.logger.error("get get_web_getCase failed {}".format(res.text))
    return retBool, ret